<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 订单接口
 */
class Order extends Api
{

    protected $noNeedLogin = ['create', 'test1'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['test2'];

    // 获取所有分类
    public function create()
    {
        $this->auth->id = 2;
        $type = $this->request->param('type'); 
     
        if(!$type){
            $this->error('参数错误');
        }
        if(!in_array($type,['course','teacher'])){
            $this->error('订单类型不存在');
        }
        $insert = [
            'sn' => date('Ymd').str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT),
            'type' => $type,
            'createtime' => time(),
        ];
        if($type == 'course'){
            $course_id = $this->request->param('course_id'); 
            $price = model("\app\admin\model\course\Course")->where('id',$course_id)->value('price');
            $insert['amount'] = $price;
            $insert['course_id'] = $course_id;
        }else{
            // 教师类型订单
        }
        $order_id = model("\app\admin\model\order\Order")->insertGetId($insert);
        if($order_id){
            $this->success('成功',['order_id'=>$order_id]);
        }else{
            $this->error('下单失败');
        }
    }

}
